package com.holystone.splunk.app.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by huyan on 2017/12/18.
 * TIME: 11:43
 * <p>
 * DESC: 多线程Demo
 */
public class RunnableDemo {

    public static void main(String[] args) {
        System.out.println("测试开始");
        demo_3();
        System.out.println("测试结束");
    }

    public static void demo_1() {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 10; i++) {
            System.out.println("start:" + new Date().getTime());
            Future<?> submit = pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("Error, InterruptedException!!");
                        e.printStackTrace();
                    }
                    System.out.println("end:" + new Date().getTime());
                }
            });
            try {
                //设置超时时间
                submit.get(2000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException interrupted) {
                submit.cancel(true);
            } catch (ExecutionException execute) {
                submit.cancel(true);
            } catch (TimeoutException timeout) {
                submit.cancel(true);
            }
        }
    }

    /**
     * 并发
     */
    public static void demo_2() {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        List<Future> list = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            System.out.println("start:" + new Date().getTime());
            Future<?> submit = pool.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.currentThread().sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println("Error, InterruptedException!!");
                        e.printStackTrace();
                    }
                    System.out.println("end:" + new Date().getTime());
                }
            });
            list.add(submit);
        }
        for (Future future : list) {
            final Future submit = future;
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        //设置超时时间
                        submit.get(2000, TimeUnit.MILLISECONDS);
                    } catch (InterruptedException interrupted) {
                        submit.cancel(true);
                    } catch (ExecutionException execute) {
                        submit.cancel(true);
                    } catch (TimeoutException timeout) {
                        submit.cancel(true);
                    }
                }
            });
            thread.start();
        }
    }

    /**
     * 测试垃圾回收
     */
    public static void demo_3() {
        while (true) {
            try {
                ExecutorService service = Executors.newFixedThreadPool(2);
                service.submit(new Runnable() {
                    public void run() {
                        System.out.println("run");
                    }
                });
                service.submit(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("shutdown");
                        service.shutdown();
                    }
                });
            } catch (Exception e) {

            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {

            }
        }
    }
}
